package com.jerry.base.io.read;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.io.FileReader;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA
 * User: Jerry
 * Date: 2024/1/24
 * Time: 19:30
 * Description:
 */
@Slf4j
public class FileReaderUsage {

    /**
     * 单个字符的读取
     *
     * @throws IOException
     */
    @Test
    public void read() throws IOException {
        String filePath = "src/main/resources/io/chinese.txt";
        try (FileReader fileReader = new FileReader(filePath)) {
            int data;
            while ((data = fileReader.read()) != -1) {
                System.out.print((char) data);
            }
        }
    }

    /**
     * 多个字符的读取
     *
     * @throws IOException
     */
    @Test
    public void read2() throws IOException {
        String filePath = "src/main/resources/io/chinese.txt";
        try (FileReader fileReader = new FileReader(filePath)) {
            char[] chars = new char[8];
            int length;
            while ((length = fileReader.read(chars)) != -1) {
                System.out.print(new String(chars, 0, length));
            }
        }
    }
}
